<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                  {required: true, message:'标题不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
				<el-col :span="12">
				    <el-form-item label="排序" prop="sort"
				        :rules="[
				          {validator: validator.isIntGteZero, trigger:'blur'}
				         ]">
				      <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
				   </el-form-item>
				</el-col>
        <!-- <el-col :span="12">
            <el-form-item label="小标题" prop="subTitile"
                :rules="[
                 ]">
              <el-input v-model="inputForm.subTitile" placeholder="请填写小标题"     ></el-input>
           </el-form-item>
        </el-col> -->

        <!-- <el-col :span="12">
            <el-form-item label="扩展字段" prop="ext"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.ext" placeholder="请填写扩展字段"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="是否上架" prop="state"
                :rules="[
                  {required: true, message:'是否上架不能为空', trigger:'blur'}
                 ]">
                    <el-radio-group v-model="inputForm.state">
                        <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="22" :offset="2" style="color: #da5353;font-size: 13px;line-height: 2;" v-show="inputForm.state === '1'">
          {{getNotice()}}
        </el-col>
        <el-col :span="12" v-show="inputForm.state=== '1'">
            <el-form-item label="上架时间" prop="upDate"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.upDate"
                      type="date"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd"
                      placeholder="选择上架时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-show="inputForm.state=== '1'">
            <el-form-item label="下架时间" prop="downDate"
                :rules="[
                 ]">
                <el-date-picker
                  v-model="inputForm.downDate"
                  type="date"
                  style="width: 100%;"
                  value-format="yyyy-MM-dd"
                  placeholder="选择下架时间">
                </el-date-picker>
           </el-form-item>
        </el-col>
    <!--    <el-col :span="24">
					<el-form-item label="参与商户" prop="merchantIds"
					     :rules="[
					      ]">
					 <GridSelect
					   title="选择商户"
					   labelName = 'name'
					   labelValue = 'id'
					   :value = "inputForm.merchantIds"
					   :limit="99999"
					   @getValue='(value, name) => {inputForm.merchantIds=value; inputForm.merchantNames=name}'
					   :columns="[
					   {
					     prop: 'name',
					     label: '商户名称'
					   },
						 {
						   prop: 'roomShow',
						   label: '铺位号'
						 }
					   ]"
					   :searchs="[
					   {
					     prop: 'name',
					     label: '商户名称'
					   }
					   ]"
					   dataListUrl="/smis/merchants/jfMerchants/list"
					   entityBeanName="jfMerchants"
					   queryEntityUrl="/smis/merchants/jfMerchants/queryById">
					 </GridSelect>
					</el-form-item>
        </el-col> -->
				<!-- <el-col :span="24">
				    <el-form-item label="数据内容" prop="content"
				        :rules="[
				         ]">
				  <el-input type="textarea" rows="4" v-model="inputForm.content" placeholder="请填写数据内容"     ></el-input>
				   </el-form-item>
				</el-col> -->
        <!-- <el-col :span="24">
          <el-form-item label="首页宣传图" prop="file['home']"
              :rules="[
               ]">
            <el-upload-ext
              :limit = "1"
              accept = ".png,.jpg,.jpeg"
              fileType = "2"
              width = "460"
              height = "268"
              v-model = "inputForm.file['home']"
              >
            </el-upload-ext>

          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="详情页轮播图" prop="file['banner']"
               :rules="[
                ]">
             <el-upload-ext
               :limit = "30"
               accept = ".png,.jpg,.jpeg"
               fileType = "2"
               width = "750"
               height = "555"
               v-model = "inputForm.file['banner']"
             >
             </el-upload-ext>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="数据内容" prop="content"
              :rules="[
               ]">
            <vue-editor useCustomImageHandler @image-added="$utils.handleImageAdded" v-model="inputForm.content"/>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="资源文件" prop="files"
                :rules="[
                 ]">
              <el-input v-model="inputForm.files" placeholder="请填写资源文件"     ></el-input>
           </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
	import ElUploadExt from '@/components/elUploadExt'
	import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          title: '',
          subTitile: '',
          content: '',
          ext: '',
          state: '',
          upDate: '',
          downDate: '',
          merchantIds: '',
          merchantNames: '',
          filesAll: '',
          file: {},
          sort: ''
        }
      }
    },
    components: {
      ElUploadExt,
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建数据`
        } else if (method === 'edit') {
          this.title = '修改数据'
        } else if (method === 'view') {
          this.title = '查看数据'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.file = {}
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/merchants/jfActivity/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfActivity)
              if(data.jfActivity.filesAll && data.jfActivity.filesAll.length > 0) {
                this.inputForm.file = JSON.parse(data.jfActivity.filesAll)
              }
              this.loading = false
            })
          } else {
						this.inputForm.state = '1'
					}
        })
      },
			getNotice() {
			  var startDate = ''
			  var endDate = ''
			  if(this.inputForm.upDate) {
			    if(this.inputForm.upDate.length === 10) {
			      startDate = this.inputForm.upDate+" 00:00:00"
			    } else {
			      startDate = this.inputForm.upDate
			    }
			  }
			  if(this.inputForm.downDate) {
			    if(this.inputForm.downDate.length === 10) {
			      endDate = this.inputForm.downDate+" 23:59:59"
			    } else  {
			      endDate = this.inputForm.downDate.replace(' 00:00:00', ' 23:59:59')
			    }
			  }
			  if(startDate !== '' && endDate !== '') {
			    return '当前数据设定的期限为：'+startDate+' ～ '+endDate
			  } else if(startDate !== '') {
			    return '当前数据设定的期限为：'+startDate+'起'
			  } else if(endDate !== '') {
			    return '当前数据设定的期限为：'+endDate+'止'
			  }
			  return '当前数据设定的期限为：长期有效'
			},
      // 表单提交
      doSubmit () {
        this.inputForm.filesAll = JSON.stringify(this.inputForm.file)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/merchants/jfActivity/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
